package com.dragon.service.impl;

import com.dragon.mapper.TrainingMapper;
import com.dragon.pojo.Problem.ProblemList;
import com.dragon.pojo.Training.*;
import com.dragon.service.TrainingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class TrainingServiceImpl implements TrainingService {
    @Autowired
    TrainingMapper trainingMapper;

    @Override
    public TrainingListResult getTrainingList(TrainingSelectInfo info) {
        info.setOffset((info.getPageNumber()-1)*info.getListNumber());
        List<TrainingList> trainingList = trainingMapper.getTrainingList(info);
        return new TrainingListResult(trainingList, trainingMapper.getSelectTotal(info));
    }

    @Override
    public Training getTrainingInfo(Integer trainingId, Integer userId) {
        return trainingMapper.getTrainingInfo(trainingId, userId);
    }

    @Override
    public List<ProblemList> getTrainingProblemList(Integer trainingId, Integer userId) {
        return trainingMapper.getTrainingProblemList(trainingId, userId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addTraining(TrainingInfo info) {

        trainingMapper.addTraining(info);

        trainingMapper.addTrainingProblem(info.getProblemIdList(), info.getTrainingId());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteTraining(Integer trainingId) {
        trainingMapper.deleteTrainingProblem(trainingId);

        trainingMapper.deleteTraining(trainingId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateTraining(TrainingInfo info) {
        trainingMapper.updateTraining(info);
        // 删除原来的题单题目
        trainingMapper.deleteTrainingProblem(info.getTrainingId());
        // 添加新的题单题目
        trainingMapper.addTrainingProblem(info.getProblemIdList(), info.getTrainingId());

    }
}
